<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像 -->
      <div class="avatar_box">
        <img src="../../assets/login.png" alt="">
      </div>
      <!-- 登录表单区域 -->
      <el-form label-width="0px" class="login_from" @keyup.enter.native="login" :model="loginFrom"
        :rules="loginFromRules" ref="loginFromRes">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input prefix-icon="el-icon-s-custom" v-model="loginFrom.username"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input type="password" prefix-icon="el-icon-lock" v-model="loginFrom.password">
          </el-input>
        </el-form-item>
        <!-- 登录和重置 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginFrom">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
 
<script>
export default {
  name: "",
  data() {
    return {
      loginFrom: {
        username: "",
        password: "",
      },
      loginFromRules: {
        username: [
          { required: true, message: "请输入登录用户名", trigger: "blur" },
          { min: 3, max: 8, message: "用户名错误请重新输入", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入登录密码", trigger: "blur" },
          { min: 3, max: 8, message: "密码错误请重新输入", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    // 点击重置表单
    resetLoginFrom() {
      // console.log(this.$refs.loginFromRes);
      this.$refs.loginFromRes.resetFields();
    },
    // 表单预验证
    login() {
      this.$refs.loginFromRes.validate(async (valid) => {
        // console.log(valid);
        if (!valid) return;
        const res = await this.$http.post("login", this.loginFrom);
        console.log(res, "res");
        // console.log(res.data.data.token);
        if (res.data.meta.status !== 200) {
          this.$message.error(res.data.meta.msg);
        } else {
          this.$message({
            message: res.data.meta.msg,
            type: "success",
          });
          window.sessionStorage.setItem("token", res.data.data.token);
          this.$router.push("/home");
        }
      });
    },
  },
};
</script>
 
<style lang="less" scoped>
.login_container {
  background-color: #025a6c;
  height: 100vh;
  .login_box {
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 5px;
    position: absolute;
    left: 50%;
    top: 50%;
    box-shadow: 0 0 10px rgb(253, 253, 253);
    transform: translate(-50%, -50%);
    .avatar_box {
      width: 130px;
      height: 130px;
      border: 1px solid #ddd;
      border-radius: 50%;
      padding: 10px;
      box-shadow: 0 0 10px rgb(62, 111, 85);
      position: absolute;
      top: -65px;
      left: 50%;
      transform: translate(-50%, 0);
      background-color: #fff;
      overflow: hidden;
      img {
        width: 478px;
        height: 478px;
        border-radius: 50%;
        border: 1px solid #eee;
        background-color: #f0f0f0;
        opacity: 0.7;
        position: absolute;
        left: -140px;
        top: -162px;
      }
    }
    .login_from {
      position: absolute;
      bottom: 0px;
      width: 90%;
      left: 50%;
      transform: translate(-50%, 0);
      .btns {
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>

